/*
 * @lc app=leetcode.cn id=69 lang=javascript
 *
 * [69] x 的平方根 
 */

// @lc code=start
/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function(x) {
    if (x === 0 || x === 1) {
        return x
    }
    let minNum = 0
    let maxNum = x
    let half = Math.floor((minNum + maxNum) / 2)
    while( !(half * half <= x) || !(x < (half + 1) * (half + 1)) ) {
        if (half * half < x) {
            minNum = half
            half = Math.floor((minNum + maxNum) / 2)
        } else if ( half * half > x ) {
            maxNum = half
            half = Math.floor((minNum + maxNum) / 2)
        }
    }
    return half
};
// @lc code=end

/* 
    牛顿迭代法是比较好的，代码简洁效率高，但是重点在于数学基础，我不会
    采用二分法搜索
*/
